//
//  BaiKeSecondClassesVC.m
//  BaseProject
//
//  Created by mac on 2020/12/22.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BaiKeSecondClassesVC.h"
#import "BSTBitmapView.h"
#import "BaiKeMainClassesModel.h"
#import "BaiKeSecondClassesItemVC.h"
#import "BaiKeSecondClassesTopView.h"
@interface BaiKeSecondClassesVC ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *titleBG;
@property(nonatomic,strong)UIScrollView *contentBG;
@property(nonatomic,strong)NSMutableArray<UILabel *> *titleBtnArrs;
@property(nonatomic,strong)NSMutableArray<UITapGestureRecognizer *> *gesArray;
@property(nonatomic,strong)UILabel *preLab;
@property(nonatomic,strong)UIView *selectedItemBottomLine;
//记录当前子控制器index
@property(nonatomic,assign)NSInteger currentIndex;
@property(nonatomic,strong)BaiKeSecondClassesTopView *topView;
//记录是否是主动筛选的
@property(nonatomic,assign)BOOL isNativeSort;
@end

@implementation BaiKeSecondClassesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createProperty];
    [self createUI];
    
}
- (void)createProperty
{
    //测试
    /**
     NSMutableArray *array = [NSMutableArray array];
     for (NSInteger index = 0; index < 30; index ++) {
         BaiKeMainClassesDataModel *secondModel = self.classesModel.data.firstObject;
         BaiKeMainClassesDataModel *jjjjj = [BaiKeMainClassesDataModel modelWithJSON:[secondModel modelToJSONObject]];
         [array addObject:jjjjj];
     }
     for (NSInteger index = 0; index < array.count; index ++) {
         NSMutableArray *array1 = [NSMutableArray array];
         for (NSInteger flag = 0; flag < 20; flag ++) {
             BaiKeMainClassesDataModel *secondModel = self.classesModel.data.firstObject.sysClassificationList.firstObject;
             BaiKeMainClassesDataModel *jjjjj = [BaiKeMainClassesDataModel modelWithJSON:[secondModel modelToJSONObject]];
             jjjjj.threeName = [NSString stringWithFormat:@"%@-%ld",jjjjj.threeName,flag];
             [array1 addObject:jjjjj];
         }
         BaiKeMainClassesDataModel *secondModel = array[index];
         secondModel.sysClassificationList = array1;
         secondModel.twoName = [NSString stringWithFormat:@"%@-%ld",secondModel.twoName,index];
     }
     self.classesModel.data = array;
     */
    //测试
    for (NSInteger index = 0; index < self.classesModel.data.count; index ++) {
        BaiKeMainClassesDataModel *secondModel = self.classesModel.data[index];
        if (index == 0) {
            secondModel.isSelected = YES;
        }
        for (NSInteger flag = 0; flag < secondModel.sysClassificationList.count; flag ++) {
            BaiKeMainClassesDataModel *threeModel = secondModel.sysClassificationList[flag];
            if (flag == 0) {
                threeModel.isSelected = YES;
            }
        }
    }
}
- (void)createUI
{
    [self topView];
    [self createContentBG];
    [self createTitleBG];
    [self createNaviBarWithTitle:self.oneName backButtonBlock:nil];
    //添加一个展位图
    if (self.titleBtnArrs.count == 0) {
        BSTBitmapView *bitmapView = [[BSTBitmapView alloc]initWithFrame:CGRectMake(0, self.topView.bottom, WIDTH, HEIGHT - self.topView.bottom)];
        [self.view addSubview:bitmapView];
        bitmapView.type = BitmapViewTypeNoData;
    }
}
#pragma mark - UI
- (void)createTitleBG
{
    //准备滑动标题
    NSMutableArray<NSString *> *nameArrs = [NSMutableArray array];
    for (NSInteger index = 0; index < self.classesModel.data.count; index ++) {
        [nameArrs addObject:self.classesModel.data[index].twoName];
    }
    //titleBG
    UIScrollView *titleBG = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.topView.bottom, WIDTH, 42)];
    [self.view addSubview:titleBG];
    titleBG.backgroundColor = [UIColor whiteColor];
    titleBG.showsVerticalScrollIndicator = NO;
    titleBG.showsHorizontalScrollIndicator = NO;
    //titleBG.delegate = self;
    //titleBG.bounces = NO;
    self.titleBG = titleBG;
    //滑动小按钮下面的横线 提前创建 比小按钮早添加到titleBG
    UIView *selectedItemBottomLine = [[UIView alloc]init];
    [titleBG addSubview:selectedItemBottomLine];
    selectedItemBottomLine.backgroundColor = rgba(255, 66, 66, 1);
    selectedItemBottomLine.layer.cornerRadius = 1.5;
    selectedItemBottomLine.layer.masksToBounds = YES;
    self.selectedItemBottomLine = selectedItemBottomLine;
    
    UITapGestureRecognizer *jumIndexTap;
    CGFloat leftMargin = 13*ADAPTER_WIDTH,orinX = leftMargin,itemMargin = 25*ADAPTER_WIDTH;
    //for循环布局
    for (NSInteger index = 0; index < nameArrs.count; index ++) {
        NSString *content = nameArrs[index];
        CGFloat itemWidth = [content sizeWithFont:[UIFont systemFontOfSize:18*ADAPTER_WIDTH weight:UIFontWeightMedium]].width;
        
        UILabel *lab = [[UILabel alloc]init];
        [titleBG addSubview:lab];
        lab.text = content;
        lab.font = [UIFont systemFontOfSize:18*ADAPTER_WIDTH weight:UIFontWeightMedium];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.frame = CGRectMake(orinX, 14, itemWidth, 17*ADAPTER_WIDTH);
        orinX = index == (nameArrs.count - 1) ? (lab.right + leftMargin) : (lab.right + itemMargin);
        //添加点击事件
        lab.tag = index;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItem:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
        //加入数组中
        [self.titleBtnArrs addObject:lab];
        [self.gesArray addObject:tap];
        if (index == self.jumIndex)
        {
            lab.font = [UIFont systemFontOfSize:18*ADAPTER_WIDTH weight:UIFontWeightMedium];
            self.preLab = lab;
            selectedItemBottomLine.frame = CGRectMake(0, titleBG.height - 5, 8, 3);
            jumIndexTap = tap;//绑定要跳转的
            lab.transform = CGAffineTransformMakeScale(1.0, 1.0);
        } else {
            lab.transform = CGAffineTransformMakeScale(0.8, 0.8);
        }
    }
    titleBG.contentSize = CGSizeMake(orinX, 0);
    //为什么在循环之外写呢 因为 头部没有创建完整 进行跳转 头部如果过长 会出现 标题不能居中问题
    if (self.titleBtnArrs.count > 0) {
        [self clickItem:jumIndexTap];
    }
}
- (void)createContentBG
{
    UIScrollView *contentBG = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 42 + self.topView.bottom, WIDTH, HEIGHT - self.topView.bottom - 42)];
    [self.view addSubview:contentBG];
    contentBG.backgroundColor = [UIColor whiteColor];
    contentBG.showsVerticalScrollIndicator = NO;
    contentBG.showsHorizontalScrollIndicator = NO;
    contentBG.pagingEnabled = YES;
    contentBG.bounces = NO;
    contentBG.delegate = self;
    self.contentBG = contentBG;
    //添加控制器
    for (NSInteger index = 0; index < self.classesModel.data.count; index ++) {
        BaiKeSecondClassesItemVC *vc = [[BaiKeSecondClassesItemVC alloc]init];
        [self addChildViewController:vc];
    }
    //这里可以添加VC的回调block之类的
    //扩大滑动范围
    contentBG.contentSize = CGSizeMake(WIDTH * self.childViewControllers.count, 0);
    for (NSInteger index = 0; index < self.childViewControllers.count; index ++)
    {
        BSTBitmapView *bitmapView = [[BSTBitmapView alloc]initWithFrame:CGRectMake(index*WIDTH, 0, WIDTH, contentBG.height)];
        [self.contentBG addSubview:bitmapView];
        bitmapView.type = BitmapViewTypeLoading;
        bitmapView.tag = 100+index;
    }
}
#pragma mark - 点击事件
- (void)clickItem:(UITapGestureRecognizer *)sender
{
    //如果animated 是YES 会走scrollView 多次 2个界面会好看些
    [self.contentBG setContentOffset:CGPointMake(sender.view.tag * WIDTH, 0) animated:NO];
    //处理控制器
    [self setUpSubController:sender.view.tag];
    //处理头部title
    [self refreshTitleLab:self.titleBtnArrs[sender.view.tag]];
}
#pragma mark - SCrollView代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / WIDTH;
    //处理控制器
    [self setUpSubController:index];
    //处理头部title
    [self refreshTitleLab:self.titleBtnArrs[index]];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / WIDTH;
    CGFloat tmpOffsetX = scrollView.contentOffset.x / WIDTH;
    CGFloat scaleRight = tmpOffsetX - index;
    CGFloat scaleLeft = 1 - scaleRight;
    if (index + 1 <= self.titleBtnArrs.count - 1)
    {
        if (self.titleBtnArrs.count == 0) {
            return;
        }
        NSLog(@"%ld,%f,%f",index,scaleRight,scaleLeft);
        UILabel *leftLab = self.titleBtnArrs[index];
        UILabel *rightLab = self.titleBtnArrs[index+1];
        if (scaleRight == 0)
        {
            //NSLog(@"到达临界值...");
            return;
        }
        if (self.currentIndex == index)
        {
            NSLog(@"向右");
            CGFloat ppWidth = rightLab.centerX - leftLab.centerX;
            CGFloat orinX = leftLab.centerX;
            self.selectedItemBottomLine.left = orinX + ppWidth * scaleRight;
            leftLab.transform = CGAffineTransformMakeScale(1.0 - (0.2 * scaleRight), 1.0 - (0.2 * scaleRight));
            rightLab.transform = CGAffineTransformMakeScale(0.8 + (0.2 * scaleRight), 0.8 + (0.2 * scaleRight));
        }
        else
        {
            NSLog(@"向左");
            CGFloat ppWidth = rightLab.centerX - leftLab.centerX;
            CGFloat orinX = rightLab.centerX;
            self.selectedItemBottomLine.left = orinX - ppWidth * scaleLeft;
            rightLab.transform = CGAffineTransformMakeScale(1.0 - (0.2 * scaleLeft), 1.0 - (0.2 * scaleLeft));
            leftLab.transform = CGAffineTransformMakeScale(0.8 + (0.2 * scaleLeft), 0.8 + (0.2 * scaleLeft));
        }
    }
}
#pragma mark - 添加子控制器视图
- (void)setUpSubController:(NSInteger)index
{
    self.currentIndex = index;
    [self changeControllerAdjustData];
    BaiKeSecondClassesItemVC *vc = self.childViewControllers[index];
    //做一些 vc的初始化
    vc.type = BaiKeSecondClassesItemVCTypeMain;
    vc.viewHeight = self.contentBG.height;
    vc.listModel = self.classesModel.data[index];
    if (vc.view.superview)
    {
        //判断是否是主动筛选
        if (self.isNativeSort) {
            [vc nativeJumpScrollTopView];
            self.isNativeSort = NO;
        }
        return;
    }
    vc.view.frame = CGRectMake(index *WIDTH, 0, WIDTH, self.contentBG.height);
    [self.contentBG addSubview:vc.view];
    
    //清除占位图
    BSTBitmapView *bitMapView = [self.contentBG viewWithTag:100+index];
    if (bitMapView != nil)
    {
        [bitMapView removeFromSuperview];
    }
}
#pragma mark - 矫正滑动标题的居中问题
- (void)refreshTitleLab:(UILabel *)currentLab
{
    self.preLab.transform = CGAffineTransformMakeScale(0.8, 0.8);
    self.preLab = currentLab;
    self.preLab.transform = CGAffineTransformMakeScale(1.0, 1.0);

    [UIView animateWithDuration:0.3 animations:^{
        self.selectedItemBottomLine.frame = CGRectMake((currentLab.left + currentLab.right - self.selectedItemBottomLine.width) * 0.5, self.selectedItemBottomLine.top, self.selectedItemBottomLine.width, self.selectedItemBottomLine.height);
    }];
    
    //标题居中
    //NSLog(@"设置居中...");
    CGFloat offsetX = currentLab.center.x - (self.titleBG.width *0.5);
    if (offsetX < 0)
    {
        offsetX = 0;
    }
    //如果针对 标签数量小 滑动范围小则出现问题 所有判断一下
    if (self.titleBG.contentSize.width > self.titleBG.width) {
        CGFloat maxOffsetX = self.titleBG.contentSize.width - self.titleBG.width;
        if (offsetX > maxOffsetX)
        {
            offsetX = maxOffsetX;
        }
    }
    //NSLog(@"offsetX:%f",offsetX);
    [self.titleBG setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
#pragma mark - 公开方法
- (void)refreshCurrentSonVC
{
    BaiKeSecondClassesItemVC *vc = self.childViewControllers[self.currentIndex];
    //[vc loadNewData];
}
#pragma mark - Lazy懒加载区域
- (NSMutableArray<UILabel *> *)titleBtnArrs
{
    if (!_titleBtnArrs)
    {
        _titleBtnArrs = [NSMutableArray array];
    }
    return _titleBtnArrs;
}
- (BaiKeSecondClassesTopView *)topView
{
    if (!_topView) {
        BaiKeSecondClassesTopView *view = [[BaiKeSecondClassesTopView alloc]initWithFrame:CGRectMake(0, kTopHeight, WIDTH, 95*ADAPTER_WIDTH)];
        [self.view addSubview:view];
        view.parentVC = self;
        view.oneName = self.oneName;
        view.classesModel = self.classesModel;
        WK(weakSelf)
        [view setCallBackSelectedBlock:^(NSInteger secondIndex, NSInteger threeIndex) {
            NSLog(@"%ld %ld",secondIndex,threeIndex);
            weakSelf.isNativeSort = YES;
            [weakSelf clickItem:weakSelf.gesArray[secondIndex]];
        }];
        _topView = view;
    }
    return _topView;
}
- (NSMutableArray<UITapGestureRecognizer *> *)gesArray
{
    if (!_gesArray) {
        _gesArray = [NSMutableArray array];
    }
    return _gesArray;
}
#pragma mark - 切换控制器 对应把数据也调整一下
//虽然有重复
- (void)changeControllerAdjustData
{
    for (NSInteger index = 0; index < self.classesModel.data.count; index ++) {
        BaiKeMainClassesDataModel *listModel = self.classesModel.data[index];
        if (index == self.currentIndex) {
            listModel.isSelected = YES;
        } else {
            listModel.isSelected = NO;
        }
    }
}
@end
